<?php
namespace Health\Http\Controllers\Admin;

use Health\Http\Controllers\Admin\BaseController;
use Health\Models\Carouse;
use Auth;
use Health\Http\Requests\CarouseRequest;

class CarouseController extends BaseController
{
    public function index()
    {
        $data['data']=Carouse::orderBy('status','desc')->orderBy('sort','desc')->paginate(10);
        return view('admin.carouse.index',$data);
    }

    public function add()
    {
        return view('admin.carouse.edit');
    }

    public function edit($id)
    {
        $data['data']=Carouse::find($id);
        return view('admin.carouse.edit',$data);
    }

    public function postEdit(CarouseRequest $request)
    {
        $data=array_map('trim', $request->only(['id','pic_link', 'status','sort']));

        $file = $request->file('img');
        if ($file&&$file->isValid()) {
            $picName = $file->getClientOriginalName();
            $file->move('images/carouse/uploads', $picName);
            $data['pic_url'] = '/images/carouse/uploads/' . $picName;
        }

        Carouse::updateOrCreate(['id'=>$data['id']],$data);
        return redirect('/carouse');

    }

    public function delete($id)
    {
        Carouse::where(['id'=>$id])->delete();
        return redirect('/carouse');
    }
}